package com.caojing.syby.ui.adapter;

import android.content.Context;

import android.content.Intent;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.caojing.syby.R;
import com.caojing.syby.bean.brand.BrandRowsBean;
import com.caojing.syby.bean.brand.BrandRowsProductInfoBean;
import com.caojing.syby.config.UrlConfig;

import com.caojing.syby.ui.brandsale.brandchilddetails.BrandChildDetailsActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by My on 2016/10/6.
 */
public class BrandChildAdapter extends BaseAdapter {

    private Context context;
    private List<BrandRowsBean> brandRowsBeen;

    public BrandChildAdapter(Context context, List<BrandRowsBean> brandRowsBeen) {
        this.context = context;
        this.brandRowsBeen = brandRowsBeen;
    }

    @Override
    public int getCount() {
        return brandRowsBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return brandRowsBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_brand_list_item, parent, false);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);

        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }

        final BrandRowsBean brandRowsBean = brandRowsBeen.get(position);
        String name = brandRowsBean.getName();
        viewHolder.tvBrandName.setText(name);
        String imgUrlSml = brandRowsBean.getImgUrlSml();
        double count = brandRowsBean.getDisCount();
        final double co = ((double)((int)(count*10))/10);
        viewHolder.tvBrandDisCount.setText(co+"折起");
        //判断是否有图片
        if (imgUrlSml!=null){
            Glide.with(context).load(UrlConfig.Path.BASE_URL+imgUrlSml).into(viewHolder.ivBrandImgUrlSml);
        }

        //获取剩余多少天计算
        String endDateStr = brandRowsBean.getEndDateStr();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        //获取当前时间
//        String format1 = format.format(new Date());
//        Log.i("tag","format1++++++++++时间"+format1 );
//        //进行时间的计算
//         long time1 = new Long("endDateStr");
//         long time2 = new Long("format1");
//         long times = time1 - time2;



//        viewHolder.tvBrandEndDateStr.setText("剩"+9+"天");


        //再次解析数据
        String productInfo = brandRowsBean.getProductInfo();

        Gson gson = new Gson();
        Type type=new TypeToken<List<BrandRowsProductInfoBean>>(){}.getType();

        //以下为图片，价格，折扣数据
        List<BrandRowsProductInfoBean> list = gson.fromJson(productInfo,type);
        BrandRowsProductInfoBean infoBean = list.get(0);
        String productImg = infoBean.getProductImg();
        double disCount = infoBean.getDisCount();
        double result = ((double)((int)(disCount*10))/10);
        viewHolder.tvBrandDisCount.setText(result+"折");

        String newPrice = infoBean.getNewPrice();

        Glide.with(context).load(productImg).into(viewHolder.ivBrandProductImgLeft);
        viewHolder.tvBrandNewPriceLeft.setText("￥"+newPrice);

        BrandRowsProductInfoBean infoBean1 = list.get(1);
        String productImg1 = infoBean1.getProductImg();
        double disCount1 = infoBean1.getDisCount();
        String newPrice1 = infoBean1.getNewPrice();

        double result1 = ((double)((int)(disCount1*10))/10);
        viewHolder.tvBrandDisCount.setText(result1+"折");

        Glide.with(context).load(productImg1).into(viewHolder.ivBrandProductImgRigthUp);
        viewHolder.tvBrandNewPriceLeft.setText("￥"+newPrice1);

        BrandRowsProductInfoBean infoBean2 = list.get(2);
        String productImg2 = infoBean2.getProductImg();
        double disCount2 = infoBean2.getDisCount();
        String newPrice2 = infoBean2.getNewPrice();

        double result2 = ((double)((int)(disCount2*10))/10);
        viewHolder.tvBrandDisCount.setText(result2+"折");

        Glide.with(context).load(productImg2).into(viewHolder.ivBrandProductImgRigthBelow);
        viewHolder.tvBrandNewPriceLeft.setText("￥"+newPrice2);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, BrandChildDetailsActivity.class);
                intent.putExtra("url", UrlConfig.Path.BASE_URL + brandRowsBeen.get(position).getImgUrlSml());
                intent.putExtra("xiu", brandRowsBeen.get(position).getName());
                intent.putExtra("zhe", co+ "折起");
                intent.putExtra("brandid", brandRowsBeen.get(position).getBrandId()+"");
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.iv_brand_ImgUrlSml)
        ImageView ivBrandImgUrlSml;
        @Bind(R.id.tv_brand_Name)
        TextView tvBrandName;
        @Bind(R.id.tv_brand_endDateStr)
        TextView tvBrandEndDateStr;
        @Bind(R.id.tv_brand_disCount)
        TextView tvBrandDisCount;
        @Bind(R.id.iv_brand_ProductImg_left)
        ImageView ivBrandProductImgLeft;
        @Bind(R.id.tv_brand_NewPrice_left)
        TextView tvBrandNewPriceLeft;
        @Bind(R.id.tv_brand_disCount_left)
        TextView tvBrandDisCountLeft;
        @Bind(R.id.iv_brand_ProductImg_rigth_up)
        ImageView ivBrandProductImgRigthUp;
        @Bind(R.id.tv_brand_NewPrice_rigth_up)
        TextView tvBrandNewPriceRigthUp;
        @Bind(R.id.tv_brand_disCount_rigth_up)
        TextView tvBrandDisCountRigthUp;
        @Bind(R.id.iv_brand_ProductImg_rigth_below)
        ImageView ivBrandProductImgRigthBelow;
        @Bind(R.id.tv_brand_NewPrice_rigth_below)
        TextView tvBrandNewPriceRigthBelow;
        @Bind(R.id.tv_brand_disCount_rigth_below)
        TextView tvBrandDisCountRigthBelow;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
